package com.hmall.search.listener;

import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ListenerItem {
    @Autowired
    ISearchService iSearchService;


    /**
     * 上架新增索引库商品
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "up.queue"),
            exchange = @Exchange(name = "item.direct",
                    type = ExchangeTypes.TOPIC),
            key = "item.upstatus"
    ))
    public void listenerUp(Long id) throws IOException {
        iSearchService.updateById(id);
    }

    /**
     * 下架删除索引库商品
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "down.queue"),
            exchange = @Exchange(name = "item.direct",
                    type = ExchangeTypes.TOPIC),
            key = "item.downstatus"
    ))
    public void listenerDown(Long id) throws IOException {
        iSearchService.downDeteleByTd(id);
    }
}
